/** Vue Router4.x版本 */
import {
	createRouter,
	createWebHashHistory,
	createWebHistory
} from 'vue-router'
const top = () => import('../components/sidemenu/TopMenu.vue');

const routes = [
	{
		path: '/',
		redirect: '/Home'
	},
	{
		path: '/',
		component: top,
		meta: {
			title: '整体页面布局'
		},
		children: [
			{
				path: '/Home',
				component: () => import('../page/home.vue'),
				meta: {
					title: '首页'
				},
			},
			{
				path: '/resource',
				component: () => import('../page/main/mainIndex.vue'),
				meta: {
					title: '资源'
				},
				redirect: '/resource/computerSystem', // 该配置是若点击选择一级菜单时，默认选中并跳转到该一级菜单下的第一个二级菜单
				children: [
					{
						path: 'Home',
						redirect: '/Home',

					},
					{
						path: 'resource',
						redirect: '/resource',

					},
					{
						path: 'wisdom',
						redirect: '/wisdom',

					},
					{
						path: 'person',
						redirect: '/person'

					},
					{
						path: 'search',
						name: '搜索结果',
						component: () => import('../page/main/searchResult.vue'),
						meta: {
							title: '搜索结果'
						},
					
					},
					{
						path: 'computerSystem',
						name: '系统',
						component: () => import('../page/main/mainContent.vue'),
						meta: {
							title: '计算机系统'
						},

					},
					{
						path: 'computerNetwork',
						name: '网络',
						component: () => import('../page/main/mainContent.vue'),
						meta: {
							title: '计算机网络'
						},
					},
					{
						path: 'dataStructure',
						name: '数据结构',
						component: () => import('../page/main/mainContent.vue'),
						meta: {
							title: '数据结构'
						},
					},
					{
						path: 'practice',
						name: '算法',
						component: () => import('../page/main/mainContent.vue'),
						meta: {
							title: '算法练习'
						},
					},
					{
						path: 'devTools',
						name: '工具',
						component: () => import('../page/main/mainContent.vue'),
						meta: {
							title: '开发工具'
						},
					},
					{
						path: 'program',
						name: '编程语言',
						component: () => import('../page/main/mainContent.vue'),
						meta: {
							title: '编程语言'
						},
					},
					{
						path: 'front-end',
						name: '前端',
						component: () => import('../page/main/mainContent.vue'),
						meta: {
							title: '前端'
						},
					},
					{
						path: 'back-end',
						name: '后端',
						component: () => import('../page/main/mainContent.vue'),
						meta: {
							title: '后端'
						},
					},
					{
						path: 'AI',
						name: 'AI',
						component: () => import('../page/main/mainContent.vue'),
						meta: {
							title: '人工智能'
						},
					},
					{
						path: 'bigData',
						name: '大数据',
						component: () => import('../page/main/mainContent.vue'),
						meta: {
							title: '大数据'
						},
					}
				]			
			},
			{
				path: '/wisdom',
				component: () => import('../page/wisdom/wisdomIndex.vue'),
				meta: {
					title: '智慧阁'
				},
				redirect: '/wisdom/wish',
				children: [{
						path: 'wish',
						name: 'wish',
						component: () => import('../page/wisdom/wisdomHouse.vue'),
						meta: {
							title: '智慧阁'
						},
					},
					{
						path: 'resource',
						redirect: '/resource'
					},
					{
						path: 'Home',
						redirect: '/Home'
					},
					{
						path: 'wisdom',
						redirect: '/wisdom'

					},
					{
						path: 'person',
						redirect: '/person'

					},
				]

			},
			{
				path: '/person',
				component: () => import('../page/person/personIndex.vue'),
				meta: {
					title: '个人中心'
				},
				redirect: '/person/Info',
				children: [{
						path: 'Info',
						component: () => import('../page/person/Info.vue'),
						meta: {
							title: '个人资料'
						},
					},
					{
						path: 'collection',
						component: () => import('../page/person/collection.vue'),
						meta: {
							title: '我的收藏'
						},
					},
					{
						path: 'recommend',
						component: () => import('../page/person/recommend.vue'),
						meta: {
							title: '推荐记录'
						},
					},
					{
						path: 'wisdomHelp',
						component: () => import('../page/person/myHelp.vue'),
						meta: {
							title: '我的求助'
						},
					},
					{
						path: 'resource',
						redirect: '/resource'
					},
					{
						path: 'Home',
						redirect: '/Home'
					},
					{
						path: 'wisdom',
						redirect: '/wisdom'

					},
					{
						path: 'person',
						redirect: '/person'

					},
				]

			},
			{
				// 详情页面
				path: '/detail',
				component: () => import('../page/main/detail.vue'),
				meta: {
					title: '详情'
				}
			},
			{
				// 推荐页面
				path: '/addResource',
				component: () => import('../page/recommend/addResource.vue'),
				meta: {
					title: '推荐'
				}
			},
			{
					path: '/npPermission',
					name:'401',
					component: () => import('../page/error/401.vue'),
					meta: {
						title: '401'
					},
			},
			{
				path: '/:pathMatch(.*)*',
				name: 'NotFound',
				component: () => import('../page/error/404.vue'),
				meta: {
					title: '404'
				}
			},
		]
	},
	{
		// 登录页面
		path: '/login',
		component: () => import('../page/Login.vue'),
		meta: {
			title: '登录'
		}
	},
	{
		// 找回密码页面
		path: '/forget',
		component: () => import('../page/findPassword.vue'),
		meta: {
			title: '找回密码'
		},
		redirect: '/forget/find',
		children: [{
				path: 'find',
				component: () => import('../page/findBody.vue'),
				meta: {
					title: '邮箱找回'
				},
			},
			{
				path: 'email',
				component: () => import('../page/forget/email.vue'),
				meta: {
					title: '邮箱找回'
				},
			}
		]
	},
	{
		path: '/403',
		component: () => import('../page/error/403.vue'),
		meta: {
			title: '403'
		}
	},
];

const router = createRouter({
	history: createWebHistory(),
	routes,
});


// 登录之前做一个判断 ,判断它进入其它页面有没有先登录,
router.beforeEach((to, from, next) => {
	// 判断有没有登录
	const isLogin = localStorage.pg_token ? true : false;
	// console.log(isLogin)
	// 如果当前访问的是登录页面或者注册页面可以让它进入
	if (to.path == '/login' || to.path == '/npPermission' || to.path == '/Home'
		|| (to.path.indexOf("/resource") == 0) || (to.path.indexOf("/wisdom") == 0)
		|| (to.path.indexOf("/forget") == 0) || (to.path.indexOf("/detail") == 0)) {
		next()
	} else {
		// 如果isLogin为true表示已经登录了;就让它正常进入就可以 ,如果没登录就让他进入登录页面
		isLogin ? next() : next('/npPermission');
	}

});

export default router;
